const connection = require('../app/database');

class searchService {
    async search(keyworld) {
        const statement = `SELECT id, title AS content, 'post' AS type FROM posts WHERE title LIKE '%${keyworld}%' OR content LIKE '%${keyworld}%'
        UNION
        SELECT id, username AS content, 'user' AS type FROM users WHERE username LIKE '%${keyworld}%'
        UNION
        SELECT tag_id, tag_name AS content, 'tag' AS type FROM tags WHERE tag_name LIKE '%${keyworld}%'
        ORDER BY id DESC;`;
        const [result] = await connection.execute(statement)
        return result
    }
}

module.exports = new searchService()

